import mongoose, { Document, Schema } from 'mongoose'

export interface IPlayback extends Document {
  roomId: mongoose.Types.ObjectId
  hostId: mongoose.Types.ObjectId
  title: string
  filename: string
  duration: number
  fileSize: number
  thumbnails: string[]
  viewCount: number
  status: 'processing' | 'ready' | 'error'
  createdAt: Date
}

const PlaybackSchema = new Schema({
  roomId: {
    type: Schema.Types.ObjectId,
    ref: 'Room',
    required: true,
  },
  hostId: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true,
  },
  title: {
    type: String,
    required: true,
  },
  filename: {
    type: String,
    required: true,
  },
  duration: {
    type: Number,
    default: 0,
  },
  fileSize: {
    type: Number,
    default: 0,
  },
  thumbnails: [String],
  viewCount: {
    type: Number,
    default: 0,
  },
  status: {
    type: String,
    enum: ['processing', 'ready', 'error'],
    default: 'processing',
  },
  createdAt: {
    type: Date,
    default: Date.now,
  },
})

// 创建索引
PlaybackSchema.index({ roomId: 1, createdAt: -1 })
PlaybackSchema.index({ hostId: 1, createdAt: -1 })
PlaybackSchema.index({ viewCount: -1 })

export const Playback = mongoose.model<IPlayback>('Playback', PlaybackSchema)
